package com.dash.dashrecoder;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class RecordListActivity extends Activity {

	private Button segmentButton = null;
	private Button uploadButton = null;
	private ListView sourceListView = null;
	private ListView segmentListView = null;
	private ProgressBar segmentProgressBar = null;
	//selectFileName
	private String selectFileName = null;
	//source file path
	private String fileList[];
	//segmet file path
	private String segmentFileList[];
	private String selectedFilePath = null;
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// full screen and no titile
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_recordlist);
		//init source listview
		sourceListView = (ListView)findViewById(R.id.listView_left);
		/*TextView textView = new TextView(this);
		textView.setText("Recoder List View");
		textView.setTextSize(20);
		sourceListView.addHeaderView(textView);*/
		initSourceListView();
		//int segment listview
		segmentListView = (ListView)findViewById(R.id.listView_right);
/*		TextView segmentTextView = new TextView(this);
		segmentTextView.setText("Segment List View");
		segmentTextView.setTextSize(20);
		segmentListView.addHeaderView(segmentTextView);*/
		initSegmentListView();
		sourceListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				selectFileName = fileList[position];
				selectedFilePath = "/mnt/sdcard/Movies/"+fileList[position];
				System.out.println("selectedFile :" + selectedFilePath);
			}
		});
		
		segmentButton = (Button)findViewById(R.id.button_segment);
		segmentButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Segment length = 3s
				Segmenter.INSTANCE.segmentVideo(selectFileName, selectedFilePath, 3.0, new UpdateProgressBar() {
					
					@Override
					public void updateSegmentProgressBar(int progress) {
						// TODO Auto-generated method stub
						segmentProgressBar.setProgress(progress);
						initSegmentListView();
					}
				});
			}
		});
		uploadButton=(Button)findViewById(R.id.button_upload);
		uploadButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(RecordListActivity.this,AsyTask.class);
				intent.putExtra("srcPaths", segmentFileList);
				//original file name
				intent.putExtra("selectFileName", selectFileName);
				for(String s:segmentFileList){
					System.out.println(s);
				}
				startActivity(intent);
			}
		});
		
		//init ProgressBar
		segmentProgressBar = (ProgressBar)findViewById(R.id.progressBar_segmentProcess);
		segmentProgressBar.setMax(100);
	}
	//init segmentListView
	private void initSegmentListView() {

		File file = new File("/mnt/sdcard/MovieSegments");
		segmentFileList = file.list();
		for(int i=0; i<segmentFileList.length; i++) {
			System.out.println("segment file path:" + segmentFileList[i]);
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, segmentFileList);
		segmentListView.setAdapter(adapter);
	}
	//init sourceListView
	private void initSourceListView() {

		File file = new File("/mnt/sdcard/Movies");
		fileList = file.list();
		for (int i = 0; i < fileList.length; i++) {
			System.out.println("soucre file path:" + fileList[i]);
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, fileList);
		sourceListView.setAdapter(adapter);
	}

}
